import Taro from '@tarojs/taro'
let isLoading = false
function request(options: Taro.request.Option, refresh = false): Promise<any> {
  if (!isLoading) {
    isLoading = true
    Taro.showLoading({ title: '加载中...', mask: true })
  }
  return new Promise(async (resolve, reject) => {
    // 请求成功后的回调
    options.success = (res) => {
      if (isLoading) {
        isLoading = false
        Taro.hideLoading()
      }
      const { data, statusCode } = res
      if (statusCode === 200) {
        resolve(data)
      } else {
        reject(data)
      }
    }
    // 请求失败后的回调
    options.fail = (res) => {
      if (isLoading) {
        isLoading = false
        Taro.hideLoading()
      }
      reject(res)
    }
    options.url = 'https://jn-ai-test-api.jingxizhigu.cn' + options.url
    Taro.request(options)
  })
}
export const listForCustomer = (data) => {
  return request({ method: 'GET', data, url: `/chat/listForCustomer` })
}
//查询
export const listAPI = (data) => {
  return request({ method: 'GET', data, url: `/chataPage/list` })
}
//新增
export const insertAPI = (data: any) => {
  return request({ method: 'POST', url: `/chataPage/insert?title=${data.title}&phone=${data.phoneNumber}` })
}

//修改
export const updateAPI = (data: any) => {
  return request({ method: 'POST', url: `/chataPage/update?id=${data.id}&title=${data.title}` })
}
//删除
export const deleteAPI = (data: any) => {
  return request({ method: 'POST', url: `/chataPage/delete?id=${data.id}` })
}
//点赞点踩
export const isSatisfiedAPI = (options: any) => {
  return request(options)
}
